package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.POJO.Permission;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    @RequestMapping("/getPermission")
    public Result getPermission(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult=permissionService.findPermisson(queryPageBean);
            return new Result(true,"查询权限成功",pageResult);
        } catch (Exception e) {
            return new Result(false,"查询权限失败");
        }
    }

    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true,"添加权限成功");
        } catch (Exception e) {
            return new Result(false,"添加权限失败");
        }
    }

}
